﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace ImagePrint
{
    /// <summary>
    /// 图片直接打印方法
    /// </summary>
    public class PrintDirectClass
    {
        public int printNum = 0;//多页打印
        public string imageFile = "";//单个图片文件
        public ArrayList fileList = new ArrayList();//多个图片文件
 
        public void PrintPreview()
        {
            PrintDocument docToPrint = new PrintDocument();

            //设置边距
            Margins margin = new Margins(20, 20, 20, 20);
            docToPrint.DefaultPageSettings.Margins = margin;
            ////纸张设置默认
            PaperSize pageSize = new PaperSize("First custom size", 800, 600);
            docToPrint.DefaultPageSettings.PaperSize = pageSize;

            docToPrint.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.docToPrint_BeginPrint);
            docToPrint.EndPrint += new System.Drawing.Printing.PrintEventHandler(this.docToPrint_EndPrint);
            docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.docToPrint_PrintPage);
            docToPrint.DefaultPageSettings.Landscape = false;
 
            PrintDialog printDialog = new PrintDialog();
            printDialog.AllowSomePages = true;
            printDialog.ShowHelp = true;
            printDialog.Document = docToPrint;
            if (printDialog.ShowDialog() == DialogResult.OK)
            {
                docToPrint.Print();
            }
        }
        private void docToPrint_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (fileList.Count == 0)
                fileList.Add(imageFile);
        }
        private void docToPrint_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
 
        }
        private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //图片抗锯齿
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
 
            Stream fs = new FileStream(fileList[printNum].ToString().Trim(), FileMode.Open, FileAccess.Read);
            System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
            int x = e.MarginBounds.X;
            int y = e.MarginBounds.Y;
            int width = image.Width;
            int height = image.Height;

          

            Rectangle destRect = new Rectangle(x, y, width, height);
            Font f = new Font("宋体", 12);
            Brush b = new SolidBrush(Color.Black);
            Graphics g = Graphics.FromImage(image);
            g.DrawString("hahahhahahahahah", f, b, 0, 25);
            e.Graphics.DrawImage(image, 0, 0, image.Width, image.Height);
           // e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
            if (printNum < fileList.Count - 1)
            {
                printNum++;
                e.HasMorePages = true;//HasMorePages为true则再次运行PrintPage事件
                return;
            }
            e.HasMorePages = false;
        }
    }
}